use log::{debug, error, info, Level,LevelFilter,trace,warn};
use std::io::Write;

fn main() {
    env_logger::Builder::new()
        .filter_level(LevelFilter::Trace) // 设置最小日志级别
        .format(|buf, record| {
            // 获取文件名（不含路径）
            let file = record.file().unwrap_or("unknown").rsplit('/').next().unwrap_or("unknown");
            let level_color = match record.level() {
                Level::Error => "\x1b[91m", // 红色
                Level::Warn => "\x1b[93m",  // 黄色
                Level::Info => "\x1b[92m",  // 绿色
                Level::Debug => "\x1b[96m", // 青色
                Level::Trace => "\x1b[90m", // 灰色
            };
            let reset_color = "\x1b[0m";
            let ts = chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.6f");
            writeln!(
                buf,
                "{ts} {level_color}[{:<5}]{reset} {}{reset} \x1b[90m{}/{}:{}\x1b[0m - {}",
                record.level().to_string(),
                level_color,  // 应用颜色到消息
                record.module_path().unwrap_or("unknown"),
                file,
                record.line().unwrap_or(0),
                record.args(),
                ts = ts,
                level_color = level_color,
                reset = reset_color,
            )
        })
        .init();

    
    trace!("this is a trace {}", "message");
    debug!("this is a debug {}", "message");
    info!("this is a info {}", "message");
    warn!("this is a warn {}", "message");
    error!("this is a warn {}", "message");

}
